﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using System.Globalization;
using System.Runtime.CompilerServices;
using Bzure.Common.Core;


namespace Bzure.Mvc.Views
{
    [FileLevelControlBuilder(typeof(BzureViewMasterPageControlBuilder))]
    public class BzureViewMasterPage : ViewMasterPage, IAreaMaster, IViewPlugin
    {
        List<IViewPlugin> ViewPlugins = new List<IViewPlugin>();
        public BzureViewMasterPage()
        {
            IViewPlugin[] vps = Actor.Public.GetManyExports<IViewPlugin>();
            if (vps.IsNotNullAndEmpty())
                this.ViewPlugins.AddRange(vps);
        }
        string _area;
        public string AreaName
        {
            get { return _area; }
            set { _area = value; SetMasterPageFile(); }
        }
        string _masterPath;
        public string MasterPath
        {
            get
            {
                return _masterPath;
            }
            set
            {
                _masterPath = value; SetMasterPageFile();
            }
        }
        [Obsolete("MasterPageFile属性已经过时，请使用Master属性设置母板相对路径", true)]
        public new string MasterPageFile
        {
            get;
            set;
        }
        void SetMasterPageFile()
        {
            if (!string.IsNullOrEmpty(_masterPath))
                base.MasterPageFile = ThemeHelper.CreateThemeViewPath(Actor.Me.ThemeName, _area, _masterPath);
        }

        internal ViewPage ViewPage
        {
            get
            {
                ViewPage page = this.Page as ViewPage;
                if (page == null)
                {
                    throw new InvalidOperationException("没有找到母板页");
                }
                return page;
            }
        }
        public Func<string> HtmlBegin
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    //sb.Append("<html>");
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.HtmlBegin());
                    }
                    return sb.ToString();
                };
            }
        }
        public Func<string> HtmlEnd
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.HtmlEnd());
                    }
                    //sb.Append("</html>");
                    return sb.ToString();
                };
            }
        }
        public Func<string> BodyBegin
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    //sb.Append("<body>");
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.BodyBegin());
                    }
                    return sb.ToString();
                };
            }
        }
        public Func<string> BodyEnd
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.BodyEnd());
                    }
                    //sb.Append("</body>");
                    return sb.ToString();
                };
            }
        }
        public Func<string> HeadBegin
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    //sb.Append("<head>");
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.HeadBegin());
                    }
                    return sb.ToString();
                };
            }
        }
        public Func<string> HeadEnd
        {
            get
            {
                return () =>
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (IViewPlugin plugin in this.ViewPlugins)
                    {
                        sb.Append(plugin.HeadEnd());
                    }
                    //sb.Append("</head>");
                    return sb.ToString();
                };
            }
        }
    }
    [FileLevelControlBuilder(typeof(BzureViewMasterPageControlBuilder))]
    public class BzureViewMasterPage<TModel> : BzureViewMasterPage
    {
        // Fields
        private AjaxHelper<TModel> _ajaxHelper;
        private HtmlHelper<TModel> _htmlHelper;
        private ViewDataDictionary<TModel> _viewData;

        // Properties
        public AjaxHelper<TModel> Ajax
        {
            get
            {
                if (this._ajaxHelper == null)
                {
                    this._ajaxHelper = new AjaxHelper<TModel>(base.ViewContext, base.ViewPage);
                }
                return this._ajaxHelper;
            }
        }

        public HtmlHelper<TModel> Html
        {
            get
            {
                if (this._htmlHelper == null)
                {
                    this._htmlHelper = new HtmlHelper<TModel>(base.ViewContext, base.ViewPage);
                }
                return this._htmlHelper;
            }
        }

        public TModel Model
        {
            get
            {
                return this.ViewData.Model;
            }
        }

        public ViewDataDictionary<TModel> ViewData
        {
            get
            {
                if (this._viewData == null)
                {
                    this._viewData = new ViewDataDictionary<TModel>(base.ViewPage.ViewData);
                }
                return this._viewData;
            }
        }

    }
}
